/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

/*
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
SPDX-FileCopyrightText: 2019 Kevin Ottens <kevin.ottens@enioka.com>
SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>

SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kcmwallpaper.h"

#include <QDebug>
#include <KAboutData>
#include <KConfigLoader>
#include <KConfigPropertyMap>
#include <KLocalizedString>
#include <KPluginFactory>
#include <KPackage/PackageLoader>
#include <QVector>
#include <KConfigPropertyMap>

#include "logging.h"

K_PLUGIN_FACTORY_WITH_JSON(WallpaperKcmFactory, "kcm_wallpaper.json", registerPlugin<WallpaperKcm>();)

static const char *s_pluginId = "org.kde.image";

WallpaperKcm::WallpaperKcm(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
    : KQuickAddons::ManagedConfigModule(parent, data, args)
{
    KAboutData *about = new KAboutData(QStringLiteral("kcm_wallpaper"),
                                       i18n("Wallpaper Control Module"),
                                       QStringLiteral("1.0"),
                                       QString(),
                                       KAboutLicense::GPL,
                                       i18n("(c) 2024 Foundui2"));
    setAboutData(about);

    constexpr const char *url = "org.kde.kcm_wallpaper.private";
    qmlRegisterAnonymousType<KCMWallpaper::WallpaperIntegration>(url, 1);
    qmlRegisterAnonymousType<KConfigPropertyMap>(url, 1);
    qmlProtectModule(url, 1);

    // Our modules will be checking the Plasmoid attached object when running from Plasma, let it load the module
    constexpr const char *uri = "org.kde.plasma.plasmoid";
    qmlRegisterUncreatableType<QObject>(uri, 2, 0, "PlasmoidPlaceholder", QStringLiteral("Do not create objects of type Plasmoid"));
   
    setButtons(NoAdditionalButton);
    loadWallpaperConfig();
}

QUrl WallpaperKcm::wallpaperConfigFile() const
{
    if (m_wallpaperIntegration) {
        return m_wallpaperIntegration->package().fileUrl(QByteArrayLiteral("ui"), QStringLiteral("config.qml"));
    } 
    return {};
}

QUrl WallpaperKcm::wallpaperMainFile() const
{
    if (m_wallpaperIntegration) {
        return m_wallpaperIntegration->package().fileUrl(QByteArrayLiteral("ui"), QStringLiteral("main.qml"));
    } 
    return {};
}

QUrl WallpaperKcm::wallpaperThumbnailsFile() const
{
    if (m_wallpaperIntegration) {
        return m_wallpaperIntegration->package().fileUrl(QByteArrayLiteral("ui"), QStringLiteral("ThumbnailsComponent.qml"));
    } 
    return {};
}

QString WallpaperKcm::currentWallpaper() const {
    return s_pluginId;
};

KCMWallpaper::WallpaperIntegration *WallpaperKcm::wallpaperIntegration() const
{
    return m_wallpaperIntegration;
}

void WallpaperKcm::loadWallpaperConfig() {
    if (m_wallpaperIntegration) { 
       qCWarning(KCM_WALLPAPER) << "wallpaperIntegration has loaded!";       
       return;
    }
    m_wallpaperIntegration = new KCMWallpaper::WallpaperIntegration(this);
    m_wallpaperIntegration->setPluginName(s_pluginId);
    m_wallpaperIntegration->init();    
    Q_EMIT currentWallpaperChanged();
}

void WallpaperKcm::setWallpaper(const QString& wallpaperPath) {  
    QDBusInterface interface("org.kde.plasmashell", "/PlasmaShell", "org.kde.PlasmaShell", QDBusConnection::sessionBus());  
    if (interface.isValid()) {
        interface.call("evaluateScript",  
            QString("var allDesktops = desktops();"  
                    "for (i=0;i<allDesktops.length;i++) {"  
                    "    var d = allDesktops[i];"  
                    "    d.wallpaperPlugin = 'org.kde.image';"  
                    "    d.currentConfigGroup = Array('Wallpaper', 'org.kde.image', 'General');"  
                    "    d.writeConfig('Image', 'file://%1');"
                    "}").arg(wallpaperPath));  
    }  
}  


#include "kcmwallpaper.moc"
